
<template>
  
<div class='clearfix'>
      <!-- 标题 -->
     <el-input
  placeholder="请输入标题"
  v-model="title"
  clearable
  class="title"
   maxlength="20"
  show-word-limit
  >
</el-input>
      <!-- 富文本编辑框 -->
      <div id="div1"
        ></div>
        
      <!-- 提交按钮 -->
      <el-button type="primary"
                 @click="addarticle"
                 class='submit'>点击上传</el-button>
                 <!-- 文本框隐藏 -->
<div>
  <textarea hidden style="width:200px; height:200px;"  v-model="content"></textarea>
</div>

        
</div>
</template>
<script>
import E from "wangeditor"; 

export default {

  data () {
    return {
     title:'',
     content:"",
     time:"",
     author:"",
     authorimg:""
    }
  },

  mounted () {
   
    const editor = new E('#div1')
    editor.config.height = 500
   
    let that=this;

   Object.assign(editor.config, {
                onchange() {
              that.content=editor.txt.html();
                  
                },
     });
     
    editor.create()
    
   
  },
 created(){
    this.author=window.sessionStorage.getItem("token");
this.getauthorimg();
 },
  methods:{
    // 添加文章
addarticle(){

  //console.log(author);//获取存储的用户ID,不需要用户自己填写
 
     let year=new Date().getFullYear();
   
  let month=new Date().getMonth()+1;
  let day=new Date().getDate();
  this.time=year+"-"+month+"-"+day;

this.$axios.post("/app/login/addarticle",{
  title:this.title,
  content:this.content,
  author:this.author,
  time:this.time,
  authorimg:this.authorimg
}).then((res)=>{
  console.log(res);
console.log(this.time);
  if(res.data.state==201){
    this.$message.success("上传文章成功")
    this.$router.go(0);
  }else{
     this.$message.error('上传失败');
  }
})
},
getauthorimg(){
    this.$axios.get("/app/login/getimg",{params:{
    xid:this.author
  }}).then((res)=>{
    // console.log(res);
    this.authorimg=res.data[0].img;
  })
}

  }
}
</script>

<style scoped>
.title{
  margin-bottom: 20px;
  width: 300px;
}
#div1{
  width: 1100px;
 margin-left: 262px;
}
.submit{
  margin-top: 20px;
}
</style>
